<?php

namespace common\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use common\models\LydbUser;

/**
 * LydbUserQuery represents the model behind the search form about `common\models\LydbUser`.
 */
class LydbUserQuery extends LydbUser
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id', 'mobile', 'create_time'], 'integer'],
            [['lydb_id', 'password', 'data', 'token'], 'safe'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = LydbUser::find();

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        if (!($this->load($params) && $this->validate())) {
            return $dataProvider;
        }

        $query->andFilterWhere([
            'id' => $this->id,
            'mobile' => $this->mobile,
        ]);

        $query->andFilterWhere(['like', 'lydb_id', $this->lydb_id])
            ->andFilterWhere(['like', 'mobile', $this->mobile])
            ->andFilterWhere(['like', 'token', $this->token]);

        return $dataProvider;
    }

    public function getUsers(){
        $query = $this->find();

        $query->limit(10);
        $data = $query->all();
        return $data;
    }

    /**
     * 随机取出一个用户
     * @return array|null|\yii\db\ActiveRecord
     */
    public function getRandOne(){
        $query = $this->find();

        $query->orderBy("rand()");

        $result = $query->one();

        return $result;
    }
}
